package com.origin.greendaolibrary;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import com.origin.greendaolibrary.greendao.DaoMaster;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.AbstractDaoMaster;
import org.greenrobot.greendao.AbstractDaoSession;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseOpenHelper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

// 入门类
public class GreenDaoManager {


    public static <T> T getWritableDaoMaster(Context context,
                                             String databaseName,
                                             Class<? extends AbstractDao<?, ?>>[] mDaoClasses,
                                             Class<? extends DatabaseOpenHelper> openHelper,
                                             Class<T> myClass

    ) {
        return getWritableDaoMaster(context, databaseName, null, mDaoClasses, null, openHelper, myClass);
    }


    public static <T> T getWritableDaoMaster(Context context,
                                             String databaseName,
                                             String databaseAddress,
                                             Class<? extends AbstractDao<?, ?>>[] mDaoClasses,
                                             String databaseEncryptPassword,
                                             Class<? extends DatabaseOpenHelper> openHelper,
                                             Class<T> myClass

    ) {

        Context mContext;

        if (TextUtils.isEmpty(databaseAddress)) {
            mContext = context;  //默认路径
        } else {
            mContext = new GreenDaoContext(context, databaseAddress);//指定路径
        }
        try {
//            Class<? extends DatabaseOpenHelper> openHelper = GreenDaoOpenHelper.class;
            Constructor<?> cons = openHelper.getConstructor(Context.class,
                    String.class, Class[].class);
            Log.e("当前是否为空==>", "mContext==" + (mContext == null));
            DatabaseOpenHelper myOpenHelper = (DatabaseOpenHelper) cons.newInstance(mContext, databaseName, mDaoClasses);
            Log.e("当前是否为空==>", "mContext== 1");
            if (TextUtils.isEmpty(databaseEncryptPassword)) {
// 不加密
                Log.e("当前是否为空==>", "mContext== 2 " + (myClass == null));
                Constructor<T> constructor = myClass.getConstructor(
                        SQLiteDatabase.class
                );
                Log.e("当前是否为空==>", "mContext== 5 " + (constructor == null));
                try {
                    constructor.newInstance(getWritableDatabase(myOpenHelper));
                } catch (Exception e) {
                    Log.e("当前是否为空==>", "mContext== 6   ....  " + e.toString());
                }

                return constructor.newInstance(getWritableDatabase(myOpenHelper));
            } else {
                Log.e("当前是否为空==>", "mContext== 3" + (myClass == null));
                return myClass.getConstructor(
                        Database.class
                ).newInstance(getEncryptedWritableDb(myOpenHelper, databaseEncryptPassword));
            }


        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
                 InvocationTargetException e) {
            e.printStackTrace();
            Log.e("初始化数据库异常", e.toString());
        }
        return null;
    }

    /**
     * 获取可写数据库
     */
    private static SQLiteDatabase getWritableDatabase(DatabaseOpenHelper devOpenHelper) {
        Log.e("当前是否为空==>", "mContext== 4" + (devOpenHelper == null));
        SQLiteDatabase sqLiteDatabase = devOpenHelper.getWritableDatabase();
        Log.e("当前是否为空==>", "mContext== 44" + (sqLiteDatabase == null));
        return sqLiteDatabase;
    }

    /**
     * 获取可写加密数据库
     */
    private static Database getEncryptedWritableDb(DatabaseOpenHelper devOpenHelper, String databaseEncryptPassword) {

        return devOpenHelper.getEncryptedWritableDb(databaseEncryptPassword);
    }


}